package generator.controller;

import com.atkt.ktkt.model.vod.Course;
import com.atkt.ktkt.vo.vod.CourseFormVo;
import com.atkt.ktkt.vo.vod.CoursePublishVo;
import com.atkt.ktkt.vo.vod.CourseQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import generator.service.CourseService;
import ktkt.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author KT917
 * @date 2022/9/18
 */

@Api(tags = "课程管理接口")
@RestController
@RequestMapping(value = "/admin/vod/course")
//@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    //点播课程列表
    @ApiOperation("点播课程列表")
    @GetMapping("/{page}/{limit}")
    public Result courseList(@PathVariable Long page,
                             @PathVariable Long limit,
                             CourseQueryVo courseQueryVo) {

        Page<Course> coursePage = new Page<>(page, limit);
        Map<String, Object> map = courseService.findPageCourse(coursePage, courseQueryVo);
        return Result.ok(map);
    }

    @ApiOperation("添加课程基本信息")
    @PostMapping("save")
    public Result save(@RequestBody CourseFormVo courseFormVo) {
        Long courseId = courseService.saveCourseInfo(courseFormVo);
        return Result.ok(courseId);
    }

    //根据id 获取课程信息
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        CourseFormVo courseFormVo = courseService.getCourseFormVoById(id);
        return Result.ok(courseFormVo);
    }

    //修改课程信息
    @PostMapping("/update")
    public Result update(@RequestBody CourseFormVo courseFormVo) {
        courseService.updateCourseId(courseFormVo);
        //返回课程id
        return Result.ok(courseFormVo.getId());
    }

    //根据课程id 查询发布课程信息
    @ApiOperation("id查询发布课程信息")
    @GetMapping("/getCoursePublishVo/{id}")
    public Result getCoursePublishVo(@PathVariable Long id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVo(id);
        return Result.ok(coursePublishVo);
    }

    //课程最终发布
    @ApiOperation("课程最终发布")
    @PutMapping("/publishCourse/{id}")
    public Result publishCourse(@PathVariable Long id) {
        courseService.getPublishCourse(id);
        return Result.ok(null);
    }

    //删除课程
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        courseService.removeCourseId(id);
        return Result.ok(null);
    }

    @ApiOperation("根据关键字查询课程")
    @GetMapping("inner/findByKeyword/{keyword}")
    public List<Course> findByKeyword(
            @ApiParam(value = "关键字", required = true)
            @PathVariable String keyword){
        QueryWrapper<Course> queryWrapper = new QueryWrapper();
        queryWrapper.like("title", keyword);
        List<Course> list = courseService.list(queryWrapper);
        return list;
    }

    @ApiOperation("根据ID查询课程")
    @GetMapping("inner/getById/{courseId}")
    public Course getById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable Long courseId){
        return courseService.getById(courseId);
    }
}
